package nl.rainbowsheep.reisorg.rest.service.controller;

import java.util.List;
import nl.rainbowsheep.reisorg.rest.service.model.City;
import nl.rainbowsheep.reisorg.rest.service.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.acls.model.NotFoundException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Controller for city
 */
@RestController
@RequestMapping("/city")
public class CityController {

    @Autowired
    private CityService cityService;

    @RequestMapping(value = "/getbyname", method = RequestMethod.GET)
    public City getCityByName(@RequestParam(value = "name", required = true) String name) throws NotFoundException {
        City city = cityService.getCityByName(name);
        if (city != null) {
            return city;
        } else {
            throw new NotFoundException(name);
        }
    }

    @RequestMapping(value = "/getall", method = RequestMethod.GET)
    @ResponseBody
    public List<City> getAllCities() throws Exception {
        List<City> cities = cityService.getAllCities();
        return cities;
    }

    @RequestMapping(value = "/getbycountry", method = RequestMethod.GET)
    @ResponseBody
    public List<City> getAllCitiesByCountry(@RequestParam(value = "country", required = true) String country) throws Exception {
        List<City> cities = cityService.getAllCitiesByCountry(country);
        return cities;
    }

    @RequestMapping(value = "/getbyregionid", method = RequestMethod.GET)
    @ResponseBody
    public List<City> getAllCitiesByRegionId(@RequestParam(value = "id", required = true) Long regionId) throws Exception {
        List<City> cities = cityService.getAllCitiesByRegionId(regionId);
        return cities;
    }

    @RequestMapping(value = "/getbycountryid", method = RequestMethod.GET)
    @ResponseBody
    public List<City> getAllCitiesByCountryId(@RequestParam(value = "id", required = true) Long countryId) throws Exception {
        List<City> cities = cityService.getAllCitiesByCountryId(countryId);
        return cities;
    }

    @RequestMapping(value = "/getbyregion", method = RequestMethod.GET)
    @ResponseBody
    public List<City> getAllCitiesByRegion(@RequestParam(value = "region", required = true) String region) throws Exception {
        List<City> cities = cityService.getAllCitiesByRegion(region);
        return cities;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public City saveCity(@RequestBody City city) throws Exception {
        //TODO: Why exacly are we catching a thrown specific exception only to throw a generic exception???
        try {
            return cityService.save(city);
        } catch (Exception ex) {
            throw new Exception();
        }
    }

}
